package org.flower.web.servlet;

import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.flower.bean.CommodityClass;
import org.flower.dao.CommodityClassDao;
import org.flower.dao.impl.CommodityClassDaoImpl;
import org.flower.service.CommodityClassService;
import org.flower.service.impl.CommodityClassServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 分类管理 servlet
 * @author Aizen
 */
@WebServlet("/manager/commodityClassServlet")
public class CommodityClassServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取点击的页码
        String pageNoStr = request.getParameter("pageNo");

        int pageNo;//获取点击的页码
        //如果不是空的就转换成int
        if(pageNoStr!=null){
            pageNo = Integer.parseInt(pageNoStr);
        }else{//如果是空的就默认给1
            pageNo = 1;
        }

        //获取模糊查询条件
        CommodityClass aClass = new CommodityClass();
        Map<String, String[]> map = request.getParameterMap();
        try {
            BeanUtils.populate(aClass,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //点击查询或子分类有一个值传过来
        String f = request.getParameter("f");
        if(f==null){
            f="";
        }
        //保存模糊查询条件
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        if(name!=null&&!name.equals("")){
            request.getSession().setAttribute("className",name);
        }
        if(type!=null&&!type.equals("")){
            request.getSession().setAttribute("classType",type);
        }
        if((type==null||type.equals(""))&&f.equals("true")){
            request.getSession().removeAttribute("classType");
        }
        if((name==null||name.equals(""))&&f.equals("true")){
            request.getSession().removeAttribute("className");
        }


        //封装对应的模糊查询条件
        String classType = (String)request.getSession().getAttribute("classType");
        if(classType!=null&&!classType.equals("")){
            aClass.setType(Integer.parseInt(classType));
        }

        //如果是查子分类
        String id = request.getParameter("id");
        //如果是点击了子分类
        if(id!=null&&!id.equals("")&&f.equals("0")){
            //存储子分类ID进行查询
            request.getSession().setAttribute("id",id);
        }else if(f!=null&&f.equals("true")){
            //如果点击了查询按钮则清除子分类查询条件
            request.getSession().removeAttribute("id");
        }

        //获取子分类ID，若不为空则按照此ID查询数据
        String id1 =(String) request.getSession().getAttribute("id");
        if(id1!=null&&!id1.equals("")){
            aClass.setPid(Integer.parseInt(id1));
        }

        CommodityClassService service = new CommodityClassServiceImpl();
        //拿分页数据
        PageInfo classPage = service.findAll(aClass,pageNo);
        //拿级别数字
        List<Integer> allType = service.findAllType();

        CommodityClassDao classDao = new CommodityClassDaoImpl();
        //父级分类名称
        List<CommodityClass> list = classPage.getList();
        for (CommodityClass c:list) {
            String pname = classDao.findPname(c.getId());
            c.setpName(pname);
        }

        classPage.setList(list);

        request.setAttribute("allType",allType);
        request.setAttribute("classPage",classPage);
        request.getRequestDispatcher("commodityClassList.jsp").forward(request,response);
    }
}
